//
//  YZMSegmentForChoose.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/28.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSegmentForChoose.h"

@interface YZMSegmentForChoose()

@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation YZMSegmentForChoose

- (instancetype)init{
    
    self = [super initWithFrame:CGRectMake(0, 0, 1, 1)];
    if (self) {
        
        _buttonArray = [NSMutableArray array];
        _currentIndex = -1;
    }
    return self;
}

- (void)setItemList:(NSArray *)itemList{
    
    _itemList = [NSArray arrayWithArray:itemList];
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (!_itemList || _itemList.count == 0) {
        
        return;
    }
    
    [_buttonArray removeAllObjects];
    
    NSMutableArray *evenArray = [NSMutableArray array];
    NSMutableArray *oddArray = [NSMutableArray array];
    [_itemList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        if (idx%2 == 0) {
            
            [evenArray addObject:obj];
            
        }else{
            
            [oddArray addObject:obj];
        }
    }];
    
    UIView *stackView = [UIView new];
    [self addSubview:stackView];
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self).offset(-6);
    }];
    
    UIView *leftView = [self stackViewWithArray:evenArray andEvenFlag:YES];
    if (leftView) {
        [stackView addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(stackView);
        }];
    }
    
    UIView *rightView = [self stackViewWithArray:oddArray andEvenFlag:NO];
    if (rightView) {
        [stackView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftView.mas_right).offset(25);
            make.top.right.equalTo(stackView);
        }];
    }else{
        
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(stackView);
        }];
    }
    
    if (_currentIndex == -1) {
        
        [self clickSegmentButton:_buttonArray[0]];
        
    }else{
        
        [self clickSegmentButton:_buttonArray[_currentIndex]];
    }
}

- (UIView *)stackViewWithArray:(NSArray *)array andEvenFlag:(BOOL)evenFlag{
    
    if (!array || array.count == 0) {
        
        return nil;
    }
    
    UIView *stackView = [UIView new];
    
    UIView *lastView = nil;
    for (NSString *title in array) {
        
        NSInteger index = [array indexOfObject:title];
        
        UIButton *button = [UIButton new];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setBackgroundColor:[UIColor clearColor]];
//        [button setImage:[UIImage imageNamed:@"icon_choose_button"] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"icon_choose_button_selected"] forState:UIControlStateSelected];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForGrayText] forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForIconColor] forState:UIControlStateSelected];
        [button.titleLabel setFont:[YZMAppStyle largeSizeFont]];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, -6)];
        [button addTarget:self action:@selector(clickSegmentButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = index*2+(evenFlag?0:1);
        [stackView addSubview:button];
        UILabel *labelInfo  = [[UILabel alloc] init];
        labelInfo.font = [YZMAppStyle mediumSizeFont];
        labelInfo.textColor = [YZMAppStyle colorForWhite];
        labelInfo.text = @"元";
        [stackView addSubview:labelInfo];
        [labelInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(stackView);
            make.right.equalTo(stackView).offset(6);
        }];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(stackView).offset(5);
            make.right.lessThanOrEqualTo(labelInfo.mas_left).offset(-6);
            if (lastView) {
                make.top.equalTo(lastView.mas_bottom).offset(20);
            }else{
                make.top.equalTo(stackView).offset(-8);
            }
        }];
        
        if (_isloss) {
            UILabel* minusLB = [YZMAppStyle labelWithLargeYellow];
            minusLB.text = @"-";
            [stackView addSubview:minusLB];
            [minusLB mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(stackView);
                make.centerY.equalTo(button);
            }];
        }
        lastView = button;
        [_buttonArray addObject:button];
    }
    
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView);
    }];
    
    return stackView;
}

- (void)clickSegmentButton:(UIButton *)sender{
    
    _currentIndex = sender.tag;
    if (self.clickAction) {
        
        self.clickAction(_currentIndex, _itemList[_currentIndex]);
    }
    
    for (UIButton *instance in _buttonArray) {
        
        if ([instance isEqual: sender]) {
            
            [instance setSelected:YES];
            
        }else{
            
            [instance setSelected:NO];
        }
    }
}


@end
